/**
 * Malsha 2014-10-29
 */

IssuerCategory = new Object();

var category_store = Ext.create('Ext.data.Store', {
	fields : [ 'id', 'code', 'issureCategory', 'primaryDealer', 'maxinvestment' ],
});

IssuerCategory.getFieldSet = function() {

	var grid = null;
	var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
		clicksToMoveEditor : 2,
		autoCancel : false
	});
	grid = Ext.create('Ext.grid.Panel', {
		layout : {
			type : 'fit'
		},
		store : category_store,
		height : 200,
		anchor : '100%',
		columns : [ {
			header : 'Issuer Category',
			dataIndex : 'issureCategory',
			width : 150,
			flex : 1
		}, {
			header : 'Category Code',
			dataIndex : 'code',
			width : 150,
			flex : 1,
			editor : {}
		}, {
			header : 'Max Investment %',
			dataIndex : 'maxinvestment',
			width : 150,
			align : 'center',
			flex : 1,
			editor : {
				xtype : 'numberfield',
				minValue : 0,
				maxValue : 100,
			}
		}, {
			header : 'Primary Dealer',
			xtype : 'booleancolumn',
			dataIndex : 'primaryDealer',
			align : 'center',
			trueText : 'Yes',
			falseText : 'No',
			width : 100,
			flex : 1
		} ],
		plugins : [ rowEditing ],
		dockedItems : [ {
			xtype : 'toolbar',
			dock : 'top',
			items : [ '->', {
				xtype : 'button',
				text : 'Add',
				listeners : {
					click : function() {
						var category = Ext.getCmp('issureCategory').getValue();
						var code = Ext.getCmp('code').getValue();
						if (category == null || category == "") {
							Ext.Msg.alert('Error', "Category can not be empty");
						} else if (code == null || code == "") {
							Ext.Msg.alert('Error', "Code can not be empty");
						} else {
							var recordIndex = category_store.findBy(function(record, id) {
								if (record.get('issureCategory') === category || record.get('code') === code) {
									return true;
								}
								return false;
							});

							if (recordIndex != -1) {
								Ext.Msg.alert('Error', "Duplicate recode fund");
							} else {
								category_store.add({
									issureCategory : category,
									code : code,
									id : 0,
									primaryDealer : Ext.getCmp('primaryDealer').getValue(),
									maxinvestment : Ext.getCmp('maxinvestment').getValue(),
								});
								this.up('form').getForm().reset();
							}
						}
					}
				}
			},
			// {
			// xtype : 'button',
			// text : 'Remove',
			// listeners : {
			// click : function() {
			// var selection =
			// grid.getView().getSelectionModel().getSelection()[0];
			// if (selection) {
			// category_store.remove(selection);
			// }
			//					}
			//				}
			//			}
			]
		} ],
		listeners : {
			render : function(p) {
				Ext.Ajax.request({
					url : 'getAllIssuerTypes.action',
					success : function(response) {
						var obj = Ext.decode(response.responseText);
						category_store.removeAll();
						category_store.loadRawData(obj.issuerTypeDtos);
					}
				});
			}
		}
	});

	return Ext.create('Ext.form.FieldSet', {
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				id : 'issureCategory',
				name : 'issureCategory',
				labelWidth : 120,
				flex : 1.5,
				padding : '0 5 0 0',
				fieldLabel : 'Issuer Category<span style="color:red">*</span>'
			}, {
				xtype : 'textfield',
				name : 'code',
				id : 'code',
				flex : 1,
				fieldLabel : 'Category Code<span style="color:red">*</span>'
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'numberfield',
				name : 'maxinvestment',
				id : 'maxinvestment',
				minValue : 0,
				maxValue : 100,
				value : 0,
				labelWidth : 120,
				padding : '0 20 0 0',
				fieldLabel : 'Max Investment %'
			}, {
				xtype : 'checkbox',
				boxLabel : 'PRIMARY DEALER',
				name : 'primaryDealer',
				inputValue : '1',
				flex : 1,
				id : 'primaryDealer'
			} ]
		}, grid ]

	});
};

IssuerCategory.getCenterPanel = function() {

	return formPanel = Ext.create('Ext.form.FormPanel', {
		title : 'Issuer Category',
		bodyPadding : '20',
		autoScroll : true,
		frame : true,
		defaults : {
			labelWidth : 175,
			anchor : '65%',
		},
		fieldDefaults : {
			labelAlign : 'left'
		},
		items : [ IssuerCategory.getFieldSet() ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
				category_store.removeAll();
				Ext.Ajax.request({
					url : 'getAllIssuerTypes.action',
					success : function(response) {
						var obj = Ext.decode(response.responseText);
						category_store.removeAll();
						category_store.loadRawData(obj.issuerTypeDtos);
					}
				});
			}
		}, {
			text : 'Save',
			id : 'save',
			handler : function() {
				var form = this.up('form').getForm();
				if (form.isValid()) {
					formPanel.setLoading('Saving...');

					var categories = [];
					category_store.each(function(record) {
						var obj = {
							"id" : record.get('id'),
							"code" : record.get("code"),
							"issureCategory" : record.get("issureCategory"),
							"primaryDealer" : record.get("primaryDealer"),
							"maxinvestment" : record.get("maxinvestment")
						};
						categories.push(obj);
					});
					form.url = 'addCategory.action';
					form.submit({
						params : {
							"categories" : Ext.encode(categories),
						},
						success : function(form, action) {
							form.reset();
							Ext.getCmp('save').setText('Save');
							category_store.removeAll();
							Ext.Msg.alert('Success', 'Saved Successfully');
							Ext.Ajax.request({
								url : 'getAllIssuerTypes.action',
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									category_store.removeAll();
									category_store.loadRawData(obj.issuerTypeDtos);
								}
							});
							formPanel.setLoading(false);
						},
						failure : function(form, action) {
							Ext.Msg.alert('Failed', 'There is some error returned from the server.');
							formPanel.setLoading(false);
						}
					});

				}
			}
		} ],
		listeners : {
			render : function(p) {
				if (App.screenPermiton.getAt(0).data.create == 0)
					Ext.getCmp('save').disable();
				else
					Ext.getCmp('save').enable();
			}
		}
	});

};